using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace ASP_TF2LogParser.UserControls
{
    public partial class MapPlotFilterControl : System.Web.UI.UserControl
    {
        protected void Page_Load( object sender, EventArgs e )
        {

            if ( !Page.IsPostBack )
            {
                LogParser.Collections.ClassList cl = new LogParser.Collections.ClassList();
                Hashtable classes = cl.GetClassList;

                ListItem li = new ListItem();
                li.Selected = true;
                li.Value = "All";
                li.Text = "All";

                ClassCheckList.Items.Add( li );


                foreach ( DictionaryEntry de in classes )
                {
                    LogParser.GameObjects.Class tempClass = (LogParser.GameObjects.Class) de.Value;
                    ClassCheckList.Items.Add( new ListItem( tempClass.LongName, tempClass.LongName ) );
                }

                int logId = Convert.ToInt32( Request.QueryString[ "logId" ].ToString() );

                List<LogParser.Kill> killList = new List<LogParser.Kill>();

                killList = LogParser.DA.KillsStatsModel.KillsSelect( logId );


                List<LogParser.Player> playerList = new List<LogParser.Player>();
                List<string> playerIds = new List<string>();
                

                foreach ( LogParser.Kill kill in killList )
                {

                    if ( !playerIds.Contains( kill.Attacker.PlayerId ) )
                    {
                        playerIds.Add( kill.Attacker.PlayerId );
                        playerList.Add( kill.Attacker );
                    }
                }

                PlayerCheckList.Items.Add( li );
                foreach ( LogParser.Player player in playerList )
                {
                    PlayerCheckList.Items.Add( new ListItem( player.PlayerNames[0], player.PlayerId ) );
                }
            }


        }

        protected void PlotMapButton_Click( object sender, EventArgs e )
        {
            if ( Request["logId"] != null )
            {
                List<string> selectedClasses = new List<string>();
                List<string> selectedPlayers = new List<string>();
                bool selectAllClasses = false;
                bool selectAllPlayers = false;

                foreach ( ListItem li in ClassCheckList.Items )
                {

                    if ( li.Selected == true )
                    {
                        if ( li.Value == "All" )
                        {
                            selectAllClasses = true;
                            selectedClasses.Clear();
                            break;
                        }

                        selectedClasses.Add( li.Value );
                    }

                }

                if ( selectAllClasses )
                {
                    foreach ( ListItem li in ClassCheckList.Items )
                    {
                        if ( li.Value != "All" )
                            selectedClasses.Add( li.Value );
                    }
                }

                foreach ( ListItem li in PlayerCheckList.Items )
                {

                    if ( li.Selected == true )
                    {
                        if ( li.Value == "All" )
                        {
                            selectAllPlayers = true;
                            selectedPlayers.Clear();
                            break;
                        }

                        selectedPlayers.Add( li.Value );
                    }
                }

                if ( selectAllPlayers )
                {
                    foreach ( ListItem li in PlayerCheckList.Items )
                    {
                        if ( li.Value != "All" )
                            selectedPlayers.Add( li.Value );
                    }
                }

                //MapPlotControl MapPlotControl1 = new MapPlotControl();

                //PlotControlPanel.Controls.Clear();
                //PlotControlPanel.Controls.Add( MapPlotControl1 );

                int logId = Convert.ToInt32( Request.QueryString[ "logId" ].ToString() );

                Session.Add( "SelectedClasses" , selectedClasses );
                Session.Add( "SelectedPlayers" , selectedPlayers );
                
                //MapPlotControl1.PlotMap( Convert.ToInt32( logId ), selectedClasses, selectedPlayers );

                PlotImage.ImageUrl = "../PlotImage.aspx?logId=" + logId.ToString();

            }
            else
            {
                ErrorLabel.Text = "No LogId specified";
            }
        }
    }
}